﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace Zj.Blog.Infrastructure.ViewEngine
{
    public class IEViewEngine : RazorViewEngine

    {
        public override ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)
        {
            this.SetViewEngine(controllerContext.HttpContext.Request);
            return base.FindPartialView(controllerContext, partialViewName, useCache);
        }

        public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
        {
            this.SetViewEngine(controllerContext.HttpContext.Request);
            return base.FindView(controllerContext, viewName, masterName, useCache);
        }

        private void SetViewEngine(HttpRequestBase request)
        {
            string engineName = "";
            var userAgent = HttpContext.Current.Request.UserAgent;
            if (userAgent.IndexOf("Chrome") > 0)
            {
                engineName= "Themes";
            }

            base.ViewLocationFormats = new string[] { "~/Views/" + engineName + "/{1}/{0}.cshtml" };
            base.PartialViewLocationFormats = new string[] { "~/Views/" + engineName + "/Shared/{1}/{0}.cshtml" };
            base.AreaViewLocationFormats = new string[] { "~/Areas/{2}/Views/" + engineName + "/{1}/{0}.cshtml" };
            base.AreaPartialViewLocationFormats = new string[] { "~/Areas/{2}/Views/" + engineName + "/Shared/{1}/{0}.cshtml" };
        }
    }
}
